/*
*	(*^。^*) TFrameWork License (*^。^*)
*/

/*
 *  Title :  
 *          
 *  Description :  
 *          
 *          
 *  Date : 
 *
 *  Author : DengPing
 *
 *  Modify : 
 */

using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

namespace TFrameWork.Module.ActionEditor
{
    public class AudioTrackStyle : CommonTrackStyle<AudioTrackStyle, AudioEventItemStyle>
    {
        public static Color AudioColor = new Color(0.98f, 0.75f, 0.03f, 1f);

        protected override void InitStyle()
        {
            Top.style.borderLeftColor = AudioColor;
            TrackMenuList.style.borderLeftColor = AudioColor;
            Texture image = EditorGUIUtility.IconContent("d_AudioClip Icon").image;
            StyleBackground style = new StyleBackground(image as Texture2D);
            Icon.style.backgroundImage = style;

        }

        public override CommonChildTrackBase CreateChildTrack()
        {
            return new CommonChildTrack();
        }

        public class CommonChildTrack : CommonChildTrackBase
        {
            public override void InitEvent()
            {
                if (_data == null || _data.events == null) return;

                for (int i = 0; i < _data.events.Count; i++)
                {
                    var eventItemStyle = new AudioEventItemStyle();
                    eventItemStyle.Init(this, _data.events[i] as AudioEvent);
                    _eventItemStyleList.Add(eventItemStyle);
                }
                
            }
            
            protected override void DragObjCreateEvent(Object obj, int selectFrameIndex)
            {
                if (obj is AudioClip clip)
                {
                    CreateAudioEvent(clip, selectFrameIndex,true);
                }
                else
                {
                    Debug.LogError("请拖拽Audio动画资源");
                }
            }

            protected override void InitTrackMouseMene(VisualElement trackParent)
            {
                // // 创建右键菜单
                var menu = new ContextualMenuManipulator(evt =>
                {
                    evt.menu.AppendAction("添加音效", AddAudio, addState);
                    addState = DropdownMenuAction.Status.Normal;
                });

                // 将右键菜单添加到 trackRoot 上
                trackParent.AddManipulator(menu);
            }

            private AudioClip AudioObject;

            public virtual void AddAudio(DropdownMenuAction obj)
            {
                ActionEditor.Instance.OpenObjectPicker<AudioClip>("t:AudioClip",go =>
                {
                    if (go != null && go is AudioClip clip)
                    {
                        var mousePos = trackParent.WorldToLocal(obj.eventInfo.mousePosition);
                        int mouseIndex =
                            Mathf.RoundToInt(mousePos.x / ActionEditor.Instance.actionEditorConfig.frameUnitWidth);
                        //Undo.RecordObject(ActionEditor.Instance.ActionData, "AudioEvent Add");
                        CreateAudioEvent(clip, mouseIndex ,false);
                    }
                });
            }

            private void CreateAudioEvent(AudioClip clip, int startIndex ,bool isDrag)
            {
                var audioEvent = DataBase.New<AudioEvent>();
                audioEvent.audioClip  = clip;
                audioEvent.audioPath = AssetDatabase.GetAssetPath(clip);
                audioEvent.durationIndex = (int)(clip.length * ActionEditor.Instance.ActionData.FrameRote);
                audioEvent.startIndex = startIndex;
                var eventItemStyle = new AudioEventItemStyle();
                eventItemStyle.Init(this, audioEvent);

                if (!isDrag)
                {
                    TUndo.RegisterCreatedObjectUndo(audioEvent, "CreateAudioEvent");
                    TUndo.RecordObject(_data, "CreateAudioEvent");
                }

                _data.AddEvent(audioEvent);
                AssetDatabase.AddObjectToAsset(audioEvent, _data);
                EditorUtility.SetDirty(_data);
                AssetDatabase.SaveAssets();
                
                _eventItemStyleList.Add(eventItemStyle);
            }
        }
    }
}
